local sample all
local var var
local RR=${masteriter}

foreach svar in capT tau {
    local fignum a
  if "`svar'"=="tau"{
    local fignum b
      }
use "${results}/temp/permute_out_stats_`sample'_${date}.dta", clear
	sort iter
	local trueval_`var'			=`var'_`svar'_1[1]
	local truevalprint_`var' 	= string(`trueval_`var'', "%05.3f")
	 qui sum `var'_`svar'_1 if iter!=0, d
	local meanval_`var'			=r(mean)
	local maxval_`var'			=r(max)
	local minval_`var'			=r(min)
	local medval_`var'			=r(p50)
	local sdval_`var'			=r(sd)
	local textcenter_`var'		=`trueval_`var''
	local meanvalprint_`var' 	= string(`meanval_`var'', "%05.3f")
	local meanvaltable_`var' 	= string(`meanval_`var'', "%09.4g")
	local medvaltable_`var' 	= string(`medval_`var'', "%09.4g")
	local sdvaltable_`var' 	= string(`sdval_`var'', "%09.4g")
	
	*Get pval as local
	sort `var'_`svar'_1
	tempvar pval_`var'
	gen `pval_`var''			=_n
	sort iter
	local tablepval_`var'		=(_N-`pval_`var''[1])/`RR'
	local pval_`var'			=`pval_`var''[1]
	if("`svar'"=="capT"){
	local xtitle "Variance of Location Effects"
	}
	if("`svar'"=="tau"){
	local xtitle "Variance of Industry-Specific Location Effects"
	}

drop if iter==0


kdensity `var'_`svar'_1, generate(tmpX tmpD) 

sum tmpD
local mx = r(max)
local pt1 = `mx'
local pt2 = floor(`pt1'*([withheld]))
local pt3 = floor(`pt1'*([withheld]))
drop tmpX tmpD

twoway ///
(kdensity `var'_`svar'_1, lcolor(black) lpattern(solid)) ///
(scatteri  0 `trueval_`var'', mcolor(none)), ///
xline(`trueval_`var'', lcolor(black) lpattern(dash)) ///
graphregion(color(white)) bgcolor(white) plotregion(style(none)) ///
legend(off) xlabel(,nogrid) ylabel(,nogrid ) ///
ytitle("Density") xtitle("`xtitle'") ///
text(`pt1'  `textcenter_`var'' "Empirical Var(`=ustrunescape("\u03BE\u0302")'{sub:l}) = " "`truevalprint_`var''",color(black) place(w)) ///
text(`pt2' `textcenter_`var'' "Permutations:" "Mean Var(`=ustrunescape("\u03BE\u0302")'{sub:l}) = " "`meanvalprint_`var''",color(black) place(w)) ///
text(`pt3' `textcenter_`var''  "Position of empirical Var(`=ustrunescape("\u03BE\u0302")'{sub:l}) in" "distribution of permutations:" ">`pval_`var''/`RR' ",color(black) place(w)) ///
 saving("${results}/Fig2`fignum'", replace)
}
